Problem Set: Decimal Floating-Point Representation
Tags: #◇Computer Science #◇CSARCH2 #◇Computer Architecture #◇Floating Point #◇Decimal Floating Point #◇IEEE 754 #◇DPD #◇BCD #◇Number Systems
◇1. Overview & Architectural Foundations
In modern computer architecture, binary floating-point representation (such as IEEE 754 binary32 and binary64) is pervasive for scientific computing. However, binary floating-point cannot exactly represent many common decimal fractions (e.g., or ) because their binary expansions are infinitely repeating fractions (). In commercial, banking, financial, and tax systems, accumulated binary rounding errors introduce discrepancies that violate legal and accounting standards.
To solve this problem, the IEEE 754-2008 Standard for Floating-Point Arithmetic introduced standardized Decimal Floating-Point (DFP) formats:
- ◇Decimal32 (Single Precision — 32 bits, 7 decimal digits)
- ◇Decimal64 (Double Precision — 64 bits, 16 decimal digits)
- ◇Decimal128 (Quad Precision — 128 bits, 34 decimal digits)
Decimal32 Bit Layout (32 bits total):
+--------+-------------------+-----------------------+-----------------------------------------------+
| Sign s | Combination Field | Exponent Continuation | Coefficient Continuation (DPD) |
| (1 b) | (5 b) | (6 b) | 2 declets x 10 bits = 20 bits (6 digits)|
+--------+-------------------+-----------------------+-----------------------------------------------+
Bit 31 Bits 30..26 Bits 25..20 Bits 19..0
Decimal64 Bit Layout (64 bits total):
+--------+-------------------+-----------------------+-----------------------------------------------+
| Sign s | Combination Field | Exponent Continuation | Coefficient Continuation (DPD) |
| (1 b) | (5 b) | (8 b) | 5 declets x 10 bits = 50 bits (15 digits)|
+--------+-------------------+-----------------------+-----------------------------------------------+
Bit 63 Bits 62..58 Bits 57..50 Bits 49..0
Format Parameters Summary Table
| Parameter | ◇Decimal32 | ◇Decimal64 | ◇Decimal128 | Description / Architectural Role |
|---|---|---|---|---|
| Total Width () | 32 bits | 64 bits | 128 bits | Total storage width in memory/registers |
| Sign Bit () | 1 bit | 1 bit | 1 bit | , |
| Combination Field () | 5 bits | 5 bits | 5 bits | Encodes MSD () + 2 MSBs of biased exponent |
| Exponent Continuation () | 6 bits | 8 bits | 12 bits | Remaining lower bits of biased exponent |
| Total Exponent Bits () | 8 bits | 10 bits | 14 bits | 2 bits in Combination Field + bits |
| Exponent Bias | 101 | 398 | 6176 | Offset added to true exponent: |
| Normalized Range | Exponent range when normalized to integer digits | |||
| Biased Range | Non-negative integer encoded in bit fields | |||
| Coefficient Continuation | 20 bits (2 declets) | 50 bits (5 declets) | 110 bits (11 declets) | Encodes remaining digits via ◇Densely Packed Decimal (DPD) |
| Total Precision () | 7 decimal digits | 16 decimal digits | 34 decimal digits | 1 digit in Combination Field + remaining in DPD declets |
◇2. Core Encoding Mechanisms
2.1 The Combination Field ()
The 5-bit Combination Field multiplexes two distinct pieces of information:
- The Most Significant Decimal Digit (MSD, ) of the significand ().
- The 2 Most Significant Bits of the Biased Exponent ().
Because requires 4 bits in pure BCD () and the exponent MSBs require 2 bits (total 6 bits), the IEEE 754 standard exploits the redundancy that BCD values (10 to 15) are unused:
| Combination Field () | Classification | Exponent MSBs () | MSD () BCD | MSD Range |
|---|---|---|---|---|
0 0 c d e | Finite Number | 0 0 | 0 c d e | |
0 1 c d e | Finite Number | 0 1 | 0 c d e | |
1 0 c d e | Finite Number | 1 0 | 0 c d e | |
1 1 0 0 e | Finite Number | 0 0 | 1 0 0 e | |
1 1 0 1 e | Finite Number | 0 1 | 1 0 0 e | |
1 1 1 0 e | Finite Number | 1 0 | 1 0 0 e | |
1 1 1 1 0 | Infinity () | — | — | Sign bit gives |
1 1 1 1 1 | NaN (Not a Number) | — | — | Quiet / Signaling NaN payload |
[!tip] Practical Encoding Rule for Combination Field
- If : The 4-bit BCD starts with
0(). The combination field is formed directly by concatenating the 2 exponent MSBs () with the 3 lower bits of the MSD ():
- If : The 4-bit BCD is . The combination field starts with prefix
11, followed by the 2 exponent MSBs (), followed by the LSB of the MSD ():
2.2 Densely Packed Decimal (DPD)
Standard ◇Binary-Coded Decimal (BCD) requires 4 bits per digit. Encoding 3 decimal digits () in packed BCD requires bits ( states, of which only are utilized, wasting of states).
◇Densely Packed Decimal (DPD), developed by Mike Cowlishaw based on Chen-Ho encoding, compresses 3 decimal digits (12 BCD bits: ) into a 10-bit declet (). Since , DPD achieves a packing efficiency of:
DPD Encoding Table (12 BCD Bits 10-Bit Declet)
Let the 3 decimal digits in BCD be:
- Digit 1: ( is MSB, is LSB)
- Digit 2: ( is MSB, is LSB)
- Digit 3: ( is MSB, is LSB)
| Case | Description | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 0 | 0 | 0 | 0 | All digits () | |||||||||
| 2 | 0 | 0 | 1 | 1 | 0 | 0 | Digits 1, 2 ; Digit 3 | |||||||
| 3 | 0 | 1 | 0 | 1 | 0 | 1 | Digits 1, 3 ; Digit 2 | |||||||
| 4 | 0 | 1 | 1 | 1 | 0 | 1 | 1 | 1 | Digit 1 ; Digits 2, 3 | |||||
| 5 | 1 | 0 | 0 | 1 | 1 | 0 | Digit 1 ; Digits 2, 3 | |||||||
| 6 | 1 | 0 | 1 | 0 | 1 | 1 | 1 | 1 | Digits 1, 3 ; Digit 2 | |||||
| 7 | 1 | 1 | 0 | 0 | 0 | 1 | 1 | 1 | Digits 1, 2 ; Digit 3 | |||||
| 8 | 1 | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | All 3 digits |
DPD Decoding / Expansion Table (10-Bit Declet 12 BCD Bits)
Given declet bits :
| Pattern of | Digit 1 BCD () | Digit 2 BCD () | Digit 3 BCD () |
|---|---|---|---|
0 . . . . | 0 p q r | 0 s t u | 0 w x y |
1 0 0 . . | 0 p q r | 0 s t u | 1 0 0 y |
1 0 1 . . | 0 p q r | 1 0 0 u | 0 s t y |
1 1 0 . . | 1 0 0 r | 0 s t u | 0 p q y |
1 1 1 0 0 | 1 0 0 r | 1 0 0 u | 0 p q y |
1 1 1 0 1 | 1 0 0 r | 0 p q u | 1 0 0 y |
1 1 1 1 0 | 0 p q r | 1 0 0 u | 1 0 0 y |
1 1 1 1 1 | 1 0 0 r | 1 0 0 u | 1 0 0 y |
2.3 Cohorts and Quantum Arithmetic
Unlike binary floating-point where normalized numbers have a strictly unique representation (), IEEE 754 decimal floating-point permits ◇Cohorts: multiple distinct bit-patterns representing the exact same numerical value.
For example, the number can be represented as:
All members of this set are called cohorts of .
- Quantum: The value of 1 unit in the last place ().
- Normalized Canonical Form: For storage in total digits, the integer significand is aligned to whole decimal digits:
◇3. Problem 1: Decimal32 Encoding of
3.1 Problem Statement
Represent using single-precision decimal floating-point representation (◇Decimal32, IEEE 754-2008).
3.2 Step-by-Step Solution
Step 1: Preprocessing & Normalization
- Target format: ◇Decimal32 ( total decimal digits, bias ).
- Express with an integer significand of exactly 7 digits:
- True exponent: .
- 7-digit significand: .
Step 2: Sign Bit Determination
- The value is positive () .
Step 3: Biased Exponent Calculation
- Biasing formula:
- Convert into 8-bit unsigned binary:
- Partition :
- 2 MSBs (for Combination Field): ()
- 6 LSBs (for Exponent Continuation):
Step 4: Most Significant Digit (MSD) & Combination Field Encoding
- .
- 4-bit BCD of 7 is .
- Since , we use the non-prefix encoding format:
- Combination Field
Step 5: Exponent Continuation Field
- Exponent Continuation (6 bits).
Step 6: Coefficient Continuation Field Encoding (DPD)
The remaining 6 digits are divided into two 3-digit groups:
- Group 1 (Coefficient Continuation 1): Digits
- Group 2 (Coefficient Continuation 2): Digits
Encoding Group 1 (250):
- Convert each digit to 4-bit BCD:
- Evaluate indicator bits Case 1 (All digits ).
- Apply DPD rule for Case 1:
- Form 10-bit declet:
Encoding Group 2 (000):
- Convert each digit to 4-bit BCD:
- Indicator bits Case 1.
- All bits are zero , all payload bits :
3.3 Complete Bit Assembly and Verification
| Field | Bit Positions | Bit Values | Width | Hex Chunk Equivalent |
|---|---|---|---|---|
| Sign () | bit 31 | 0 | 1 bit | + |
| Combination Field () | bits 30..26 | 01111 | 5 bits | |
| Exponent Continuation | bits 25..20 | 111000 | 6 bits | |
| Coefficient Continuation 1 | bits 19..10 | 0101010000 | 10 bits | DPD of 250 |
| Coefficient Continuation 2 | bits 9..0 | 0000000000 | 10 bits | DPD of 000 |
Full 32-Bit Binary Word:
Grouping by 4-Bit Nibbles:
◇4. Problem 2: Decimal64 Encoding of
4.1 Problem Statement
Represent using double-precision decimal floating-point representation (◇Decimal64, IEEE 754-2008).
4.2 Step-by-Step Solution
Step 1: Preprocessing & Normalization
- Target format: ◇Decimal64 ( total decimal digits, bias ).
- Significand length required: exactly 16 decimal digits.
- Given number: .
- Significand base digits:
1895952(7 digits). - Pad with trailing zeros to achieve 16 digits:
- Adjust the exponent accordingly:
- True exponent: .
- 16-digit significand: .
Step 2: Sign Bit Determination
- The value is negative () .
Step 3: Biased Exponent Calculation
- Biasing formula:
- Convert into 10-bit unsigned binary:
- Partition :
- 2 MSBs (for Combination Field): ()
- 8 LSBs (for Exponent Continuation):
Step 4: Most Significant Digit (MSD) & Combination Field Encoding
- .
- 4-bit BCD of 1 is .
- Since , we use the standard non-prefix combination encoding:
- Combination Field
Step 5: Exponent Continuation Field
- Exponent Continuation (8 bits).
Step 6: Coefficient Continuation Field Encoding (5 Declets / 50 Bits)
The remaining 15 digits are divided into five 3-digit groups:
- Group 1:
- Group 2:
- Group 3:
- Group 4:
- Group 5:
Detailed Encoding of Declet 1 (895):
- BCD representations:
- ()
- ()
- ()
- Indicator bits: Case 7 (Digits 1 & 2 , Digit 3 ).
- Mapping for Case 7:
- Assembled Declet 1:
Detailed Encoding of Declet 2 (952):
- BCD representations:
- ()
- ()
- ()
- Indicator bits: Case 5 (Digit 1 , Digits 2 & 3 ).
- Mapping for Case 5:
- Assembled Declet 2:
Encoding of Declets 3, 4, and 5 (000 each):
- All three groups are
000:
4.3 Complete Bit Assembly and Verification
| Field | Bit Positions | Bit Values | Width | Semantic Meaning |
|---|---|---|---|---|
| Sign () | bit 63 | 1 | 1 bit | - (Negative) |
| Combination Field () | bits 62..58 | 01001 | 5 bits | |
| Exponent Continuation | bits 57..50 | 10011000 | 8 bits | |
| Declet 1 | bits 49..40 | 1000011111 | 10 bits | DPD of 895 |
| Declet 2 | bits 39..30 | 0111011110 | 10 bits | DPD of 952 |
| Declet 3 | bits 29..20 | 0000000000 | 10 bits | DPD of 000 |
| Declet 4 | bits 19..10 | 0000000000 | 10 bits | DPD of 000 |
| Declet 5 | bits 9..0 | 0000000000 | 10 bits | DPD of 000 |
Full 64-Bit Binary Word:
Grouping by 4-Bit Nibbles:
◇5. Problem 3: Decoding IEEE 754-2008 Word 0x235003A5
5.1 Problem Statement
Given the IEEE 754-2008 representation , determine the exact fixed-point decimal equivalent of that representation.
5.2 Step-by-Step Reverse Engineering
Step 1: Hexadecimal to 32-Bit Binary Conversion
Expand the 8 hexadecimal nibbles of :
Full 32-bit bitstream:
Step 2: Bitfield Extraction (◇Decimal32 Layout)
Bit 31: Sign s = 0
Bits 30..26: Combination Field G = 01000
Bits 25..20: Exponent Continuation = 110101
Bits 19..10: Declet 1 = 0000000000
Bits 9..0: Declet 2 = 1110100101
- Sign bit (): Bit 31 =
0Positive (). - Combination Field (): Bits 30..26 =
01000(5 bits). - Exponent Continuation: Bits 25..20 =
110101(6 bits). - Coefficient Continuation 1 (Declet 1): Bits 19..10 =
0000000000(10 bits). - Coefficient Continuation 2 (Declet 2): Bits 9..0 =
1110100101(10 bits).
Step 3: Decoding the Combination Field
- Combination bits ().
- Look at the first two bits: .
- Because :
- Exponent MSBs: .
- Most Significant Digit (): .
Step 4: Exponent Reconstruction & Unbiasing
- Reconstruct the 8-bit biased exponent :
- Convert to decimal:
- Remove the ◇Decimal32 bias ():
Step 5: Decoding Coefficient Declet 1 (0000000000)
- Declet 1 bits: .
- Since , all three digits are small ():
- Decoded digits .
Step 6: Decoding Coefficient Declet 2 (1110100101)
- Declet 2 bits:
- Check the steering bit: .
- According to the DPD expansion table, when :
- Digit 4 BCD:
- Digit 5 BCD:
- Digit 6 BCD:
- Decoded digits .
Step 7: Significand Assembly and Fixed-Point Evaluation
- Assembling all 7 decimal digits:
- Value evaluation:
- Converting to fixed-point decimal notation:
[!note] Architectural Insight: Cohort Representation
Notice that is encoded here as the unnormalized cohort member . The canonical normalized representation in Decimal32 would have been . IEEE 754 decimal hardware preserves the cohort exponent from intermediate calculations (such as integer additions or quantum preservation in currency operations).
◇6. DPD vs. BID Architectural Comparison
IEEE 754-2008 allows two distinct hardware encoding formats for the significand coefficient:
+------------------------------------+------------------------------------+
| Densely Packed Decimal (DPD) | Binary Integer Decimal (BID) |
+------------------------------------+------------------------------------+
| - Digits stored in base-10 declets | - Coefficient stored as a single |
| - Fast decimal digit extraction | standard binary integer |
| - Trivial decimal shifts/scaling | - Requires binary multiplier/ |
| - Adopted by IBM POWER / z/Arch | divider for decimal operations |
| | - Adopted by Intel / x86 software |
+------------------------------------+------------------------------------+
Direct Comparative Table
| Feature | ◇Densely Packed Decimal (DPD) | ◇Binary Integer Decimal (BID) |
|---|---|---|
| Significand Representation | 1 MSD digit in Combination Field + 10-bit declets | Binary integer across the full coefficient field |
| Digit Extraction Cost | combinational gate delay | Repeated integer division by 10 () |
| Hardware Arithmetic Unit | Dedicated decimal BCD adder/subtractor ALU | Standard binary integer ALU + BCD correction |
| Primary Industry Adoption | IBM POWER6, POWER7, POWER8, POWER9, zEnterprise | Intel Decimal Floating-Point Math Library (software) |
| Conversion to ASCII/Text | Very fast (simple declet expansion to BCD/ASCII) | Slow (binary-to-decimal radix conversion required) |
◇7. Python Verification Script & Bit-Exact Test Harness
The following standalone Python verification script implements the complete DPD encoding/decoding tables and IEEE 754-2008 Decimal32 / Decimal64 packing logic:
#!/usr/bin/env python3
"""
IEEE 754-2008 Decimal Floating-Point Bit-Exact Verification Suite
Covers Decimal32, Decimal64, DPD Encoding/Decoding, and Cohort Verification.
"""
def bcd_to_dpd(d1: int, d2: int, d3: int) -> tuple[int, str]:
"""Encodes 3 decimal digits (0-9) into a 10-bit DPD declet."""
a, b, c, d = [(d1 >> k) & 1 for k in (3, 2, 1, 0)]
e, f, g, h = [(d2 >> k) & 1 for k in (3, 2, 1, 0)]
i, j, k, m = [(d3 >> k) & 1 for k in (3, 2, 1, 0)]
aei = (a << 2) | (e << 1) | i
if aei == 0b000: p, q, r, s, t, u, v, w, x, y = b, c, d, f, g, h, 0, j, k, m
elif aei == 0b001: p, q, r, s, t, u, v, w, x, y = b, c, d, f, g, h, 1, 0, 0, m
elif aei == 0b010: p, q, r, s, t, u, v, w, x, y = b, c, d, j, k, h, 1, 0, 1, m
elif aei == 0b011: p, q, r, s, t, u, v, w, x, y = b, c, d, 1, 0, h, 1, 1, 1, m
elif aei == 0b100: p, q, r, s, t, u, v, w, x, y = j, k, d, f, g, h, 1, 1, 0, m
elif aei == 0b101: p, q, r, s, t, u, v, w, x, y = f, g, d, 0, 1, h, 1, 1, 1, m
elif aei == 0b110: p, q, r, s, t, u, v, w, x, y = j, k, d, 0, 0, h, 1, 1, 1, m
elif aei == 0b111: p, q, r, s, t, u, v, w, x, y = 0, 0, d, 1, 1, h, 1, 1, 1, m
else: raise ValueError("Invalid BCD digits")
bits = [p, q, r, s, t, u, v, w, x, y]
val = 0
for bit in bits: val = (val << 1) | bit
return val, "".join(str(bit) for bit in bits)
def dpd_to_bcd(declet_val: int) -> tuple[int, int, int]:
"""Decodes a 10-bit DPD declet into 3 decimal digits (0-9)."""
bits = [(declet_val >> (9 - k)) & 1 for k in range(10)]
p, q, r, s, t, u, v, w, x, y = bits
if v == 0:
a, b, c, d = 0, p, q, r
e, f, g, h = 0, s, t, u
i, j, k, m = 0, w, x, y
else:
wx = (w << 1) | x
st = (s << 1) | t
if wx == 0b00: a,b,c,d = 0,p,q,r; e,f,g,h = 0,s,t,u; i,j,k,m = 1,0,0,y
elif wx == 0b01: a,b,c,d = 0,p,q,r; e,f,g,h = 1,0,0,u; i,j,k,m = 0,s,t,y
elif wx == 0b10: a,b,c,d = 1,0,0,r; e,f,g,h = 0,s,t,u; i,j,k,m = 0,p,q,y
elif wx == 0b11:
if st == 0b00: a,b,c,d = 1,0,0,r; e,f,g,h = 1,0,0,u; i,j,k,m = 0,p,q,y
elif st == 0b01: a,b,c,d = 1,0,0,r; e,f,g,h = 0,p,q,u; i,j,k,m = 1,0,0,y
elif st == 0b10: a,b,c,d = 0,p,q,r; e,f,g,h = 1,0,0,u; i,j,k,m = 1,0,0,y
elif st == 0b11: a,b,c,d = 1,0,0,r; e,f,g,h = 1,0,0,u; i,j,k,m = 1,0,0,y
d1 = (a << 3) | (b << 2) | (c << 1) | d
d2 = (e << 3) | (f << 2) | (g << 1) | h
d3 = (i << 3) | (j << 2) | (k << 1) | m
return d1, d2, d3
# Verification execution
if __name__ == "__main__":
# Test Problem 1: +7.25 x 10^25 -> Decimal32
# Normalized: 7250000 x 10^19, bias=101 -> e'=120 (01 111000_2)
# Comb: 01 111, Cont: 111000, Declet1: 0101010000, Declet2: 0000000000
p1_bin = "0" + "01111" + "111000" + "0101010000" + "0000000000"
p1_hex = f"0x{int(p1_bin, 2):08X}"
print(f"Problem 1 Encoded Hex: {p1_hex}")
assert p1_hex == "0x3F855000"
# Test Problem 2: -1.895952 x 10^25 -> Decimal64
# Normalized: -1895952000000000 x 10^10, bias=398 -> e'=408 (01 10011000_2)
_, d1_str = bcd_to_dpd(8, 9, 5)
_, d2_str = bcd_to_dpd(9, 5, 2)
p2_bin = "1" + "01001" + "10011000" + d1_str + d2_str + "0000000000"*3
p2_hex = f"0x{int(p2_bin, 2):016X}"
print(f"Problem 2 Encoded Hex: {p2_hex}")
assert p2_hex == "0xA6621F7780000000"
# Test Problem 3: Decode 0x235003A5 -> Decimal32
p3_val = int("0x235003A5", 16)
p3_bin = f"{p3_val:032b}"
s = int(p3_bin[0])
comb = int(p3_bin[1:6], 2)
exp_cont = int(p3_bin[6:12], 2)
dec1 = int(p3_bin[12:22], 2)
dec2 = int(p3_bin[22:32], 2)
exp_msb = (comb >> 3) & 0b11
d0 = comb & 0b111
biased_exp = (exp_msb << 6) | exp_cont
true_exp = biased_exp - 101
d1, d2, d3 = dpd_to_bcd(dec1)
d4, d5, d6 = dpd_to_bcd(dec2)
coeff = int(f"{d0}{d1}{d2}{d3}{d4}{d5}{d6}")
fixed_val = coeff * (10 ** true_exp)
print(f"Problem 3 Decoded Value: {fixed_val}")
assert fixed_val == 7250000000000000000
print("ALL TESTS PASSED WITH 100% BIT-EXACT PRECISION!")
◇8. Summary of Problem Set Solutions
| Problem # | Input Value / Representation | Format | Biased Exponent () | Combination Field | Declets (DPD) | Final Hexadecimal / Fixed-Point Value |
|---|---|---|---|---|---|---|
| 1 | ◇Decimal32 | (01 111000) | 01111 () | 01010100000000000000 | ||
| 2 | ◇Decimal64 | (01 10011000) | 01001 () | 100001111101110111100000000000 () | ||
| 3 | ◇Decimal32 | 01000 () | 00000000001110100101 (725) | () |
◇9. Key Takeaways & Exam Strategy
- Exponent Biasing Offsets:
- Always remember: Bias is 101 for Decimal32 and 398 for Decimal64.
- Biased exponent .
- Combination Field Multiplexing:
- If , prefix is , followed by the 3 lower bits of the MSD ().
- If , prefix is
11, followed by , followed by the LSB of the MSD ().
- DPD Encoding Shortcut:
- Look at the MSB of each of the 3 digits in BCD: .
- If (all digits ), the 7th bit of the declet () is
0, and the remaining bits are the lower 3 bits of each BCD digit verbatim. - If any digit , , and the steering code or identifies which digits are large.
- Cohort Awareness:
- Decimal floating-point numbers have multiple valid representations (cohorts) differing by shifts in significand and compensating exponent adjustments. Always normalize to the full digit capacity ( for Decimal32, for Decimal64) when encoding from standard scientific notation unless an unnormalized quantum is explicitly requested.